﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PreallocateRuleBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IPreallocateRuleDA preallocateRuleDA = DataAccess.CreatePreallocateRuleDA();
        private static readonly IPreallocateRuleDetailDA preallocateRuleDetailDA = DataAccess.CreatePreallocateRuleDetailDA();
        private PubParasInfo pubParasInfo;

        public PreallocateRuleBLL()
        {
            this.pubParasInfo = null;
        }

        public PreallocateRuleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PreallocateRuleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<PreallocateRuleInfo> preallocateRuleInfos = new List<PreallocateRuleInfo>();
                PreallocateRuleInfo preallocateRuleInfo = null;
                foreach (string id in IDS)
                {
                    preallocateRuleInfo = preallocateRuleDA.GetPreallocateRuleByID(dataBase, tran, id, this.dataBaseID[0]);
                    preallocateRuleInfo.PreallocateRuleDetailInfos = preallocateRuleDetailDA.GetPreallocateRuleDetailListByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    preallocateRuleInfos.Add(preallocateRuleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                preallocateRuleDetailDA.DeletePreallocateRuleDetail(dataBase, tran, IDS, this.dataBaseID);
                preallocateRuleDA.DeletePreallocateRule(dataBase, tran, IDS, this.dataBaseID);
                for (int i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<PreallocateRuleInfo>(dataBase, tran, preallocateRuleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PREALLOCATE_RULE", this.dataBaseID[0]);
                    if ((preallocateRuleInfos[i].PreallocateRuleDetailInfos != null) && (preallocateRuleInfos[i].PreallocateRuleDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < preallocateRuleInfos[i].PreallocateRuleDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<PreallocateRuleDetailInfo>(dataBase, tran, preallocateRuleInfos[i].PreallocateRuleDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "PREALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return preallocateRuleDA.GetAllPreallocateRule(this.dataBaseID[0]);
        }

        public object GetInfoByID(string preallocateRuleID)
        {
            PreallocateRuleInfo preallocateRuleInfo = preallocateRuleDA.GetPreallocateRuleByID(preallocateRuleID, this.dataBaseID[0]);
            if (preallocateRuleInfo != null)
            {
                preallocateRuleInfo.PreallocateRuleDetailInfos = preallocateRuleDetailDA.GetPreallocateRuleDetailListByID(preallocateRuleInfo.PreallocateRuleID, this.dataBaseID[0], this.pubParasInfo);
            }
            return preallocateRuleInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            PreallocateRuleQueryEntity preallocateRuleQuery = obj as PreallocateRuleQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(preallocateRuleQuery.PreallocateRuleID))
            {
                sqlWhere.Add(" PREALLOCATE_RULE_ID " + preallocateRuleQuery.PreallocateRuleID);
            }
            if (!string.IsNullOrEmpty(preallocateRuleQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + preallocateRuleQuery.Descr);
            }
            if (!string.IsNullOrEmpty(preallocateRuleQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + preallocateRuleQuery.IsActive);
            }
            return preallocateRuleDA.GetPreallocateRuleByQueryList(sqlWhere, preallocateRuleQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string preallocateRuleID = string.Empty;
            try
            {
                PreallocateRuleInfo preallocateRule = obj as PreallocateRuleInfo;
                if (preallocateRuleDA.CheckPreallocateRuleIDUnique(dataBase, tran, preallocateRule.PreallocateRuleID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2430_001", this.pubParasInfo.UserLanguage, new List<string> { preallocateRule.PreallocateRuleID });
                }
                preallocateRule.UpdatedBy = preallocateRule.CreatedBy;
                preallocateRule.CreatedDate = DateTime.Now;
                preallocateRule.UpdatedDate = DateTime.Now;
                preallocateRuleDA.InsertPreallocateRule(dataBase, tran, preallocateRule, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<PreallocateRuleInfo>(dataBase, tran, null, preallocateRule, AuditAction.Insert, this.pubParasInfo.UserID, "PREALLOCATE_RULE", this.dataBaseID[0]);
                preallocateRuleID = preallocateRule.PreallocateRuleID;
                if ((preallocateRule.PreallocateRuleDetailInfos != null) && (preallocateRule.PreallocateRuleDetailInfos.Count > 0))
                {
                    for (int i = 0; i < preallocateRule.PreallocateRuleDetailInfos.Count; i++)
                    {
                        if (preallocateRuleDetailDA.CheckPreallocateRuleDetailIDUnique(dataBase, tran, preallocateRule.PreallocateRuleDetailInfos[i].PreallocateRuleID, preallocateRule.PreallocateRuleDetailInfos[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2430_002", this.pubParasInfo.UserLanguage, new List<string> { preallocateRule.PreallocateRuleDetailInfos[i].PreallocateRuleID, preallocateRule.PreallocateRuleDetailInfos[i].LineID.ToString() });
                        }
                        else
                        {
                            preallocateRule.PreallocateRuleDetailInfos[i].CreatedDate = DateTime.Now;
                            preallocateRule.PreallocateRuleDetailInfos[i].UpdatedDate = DateTime.Now;
                            preallocateRule.PreallocateRuleDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                            preallocateRule.PreallocateRuleDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                            preallocateRuleDetailDA.InsertPreallocateRuleDetail(dataBase, tran, preallocateRule.PreallocateRuleDetailInfos[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<PreallocateRuleDetailInfo>(dataBase, tran, null, preallocateRule.PreallocateRuleDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "PREALLOCATE_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return preallocateRuleID;
        }

        public string Update(object obj)
        {
            string preallocateRuleID = string.Empty;
            try
            {
                PreallocateRuleInfo preallocateRule = obj as PreallocateRuleInfo;
                preallocateRule.UpdatedDate = DateTime.Now;
                PreallocateRuleInfo oldPreallocateRuleInfo = preallocateRuleDA.GetPreallocateRuleByID(preallocateRule.PreallocateRuleID, this.dataBaseID[0]);
                if (preallocateRuleDA.UpdatePreallocateRule(preallocateRule, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PreallocateRuleInfo>(oldPreallocateRuleInfo, preallocateRule, AuditAction.Update, this.pubParasInfo.UserID, "PREALLOCATE_RULE", this.dataBaseID[0]);
                    preallocateRuleID = preallocateRule.PreallocateRuleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return preallocateRuleID;
        }
    }
}

